package com.atguigu.gmall.payment.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.service.AlipayService;
import com.atguigu.gmall.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Auther 赵乾坤
 * @Date 2021-01-11 22:34
 */
@Controller
@RequestMapping("/api/payment/alipay")
public class AlipayController {

    @Autowired
    private AlipayService alipayService;
    @Autowired
    private PaymentService paymentService;//

    //去支付宝进行支付
    @GetMapping("/submit/{orderId}")
    @ResponseBody
    public String submitAlipay(@PathVariable Long orderId) {
        return alipayService.submitAlipay(orderId);
    }

    //同步通知
    @GetMapping("/callback/return")
        public String callbackReturn(){
            return "redirect:" + AlipayConfig.return_order_url;
        }

    //支付宝公司异步通知
    @PostMapping("/callback/notify")
    @ResponseBody
    public String callbackNotify(@RequestParam Map<String,String> paramsMap) {
        System.out.println("+++++++");
        try {
            boolean signVerified = AlipaySignature.rsaCheckV1(
                    paramsMap,
                    AlipayConfig.alipay_public_key,
                    AlipayConfig.charset,
                    AlipayConfig.sign_type);
            if(signVerified){
                //  验签成功后，按照支付结果异步通知中的描述，对支付结果中的业务内容进行二次校验，校验成功后在response中返回success并继续商户自身业务处理，校验失败返回failure
                if ("TRADE_SUCCESS".equals(paramsMap.get("trade_status"))) {//交易状态
                    //交易成功
                    paymentService.updatePaymentInfo(paramsMap);
                }//
                System.out.println(paramsMap);
                //{gmt_create=2021-01-12 09:50:50,
                // charset=utf-8,
                // gmt_payment=2021-01-12 09:50:58,
                // notify_time=2021-01-12 09:50:59,
                // subject=Apple iPhone 11 (A2223) 128GB 黄色 移动联通电信4G手机 双卡双待 Apple iPhone 11 (A2223) 128GB 黑色 移动联通电信4G手机 双卡双待 ,
                // buyer_id=2088102181130586,
                // invoice_amount=23996.00,
                // version=1.0,
                // notify_id=2021011200222095058030580512101259,
                // fund_bill_list=[{"amount":"23996.00","fundChannel":"ALIPAYACCOUNT"}],
                // notify_type=trade_status_sync,
                // out_trade_no=ATGUIGU161041623884913,
                // total_amount=23996.00,
                // trade_status=TRADE_SUCCESS,
                // trade_no=2021011222001430580501354428,
                // auth_app_id=2016102100732915,
                // receipt_amount=23996.00,
                // point_amount=0.00,
                // app_id=2016102100732915,
                // buyer_pay_amount=23996.00,
                // seller_id=2088102180533564}


                return "success";

            }else{
                //  验签失败则记录异常日志，并在response中返回failure.
                System.out.println("验签失败");
                return "failure";
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return "failure";

    }
}
